package com.droid.lib.controls.demo.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cocosw.bottomsheet.BottomSheet;
import com.cocosw.bottomsheet.BottomSheetHelper;
import com.droid.lib.controls.demo.R;
import com.droid.lib.controls.demo.adapter.MenuAdapter;
import com.droid.lib.controls.demo.adapter.MenuAdapter.OnClicker;
import com.droid.lib.controls.demo.view.AllChartFragment;
import com.droid.lib.controls.demo.view.BottomBarFragment;
import com.droid.lib.controls.demo.view.BottomSheetFragmentDemo;
import com.droid.lib.controls.demo.view.CalendarFragment;
import com.droid.lib.controls.demo.view.CardsFragment;
import com.droid.lib.controls.demo.view.DialogsFragment;
import com.droid.lib.controls.demo.view.FloatLabelFragment;
import com.droid.lib.controls.demo.view.FloatingActionFragment;
import com.droid.lib.controls.demo.view.Lock9Fragment;
import com.droid.lib.controls.demo.view.ProgressFragment;
import com.droid.lib.controls.demo.view.SnackBarFragment;
import com.droid.lib.controls.demo.view.SpinnerFragment;
import com.droid.lib.controls.demo.view.ToastFragment;
import com.droid.lib.controls.demo.view.TreeFragment;
import com.droid.lib.controls.demo.util.LogicUtils;
import com.droid.lib.controls.demo.widget.ResideMenu;
import com.droid.lib.controls.demo.widget.ResideMenu.OnMenuListener;
import com.droid.lib.controls.widget.RippleImageView;
import com.github.zackratos.ultimatebar.UltimateBar;
import java.util.ArrayList;


public class MainActivity extends FragmentActivity implements LogicUtils {

    private Context mContext;
    private ResideMenu mResideMenu;
    private TextView mtvTitleName;
    private RippleImageView mbtnBack;
    private ListView mlvListMinu;
    private ArrayList<String> marrList;
    private MenuAdapter mMenuAdapter;

    private static final int TAG_1 = 0;
    private static final int TAG_2 = 1;
    private static final int TAG_3 = 2;
    private static final int TAG_4 = 3;
    private static final int TAG_5 = 4;

    private static final int TAG_6 = 5;
    private static final int TAG_7 = 6;
    private static final int TAG_8 = 7;
    private static final int TAG_9 = 8;
    private static final int TAG_10 = 9;
    private static final int TAG_11 = 10;
    private static final int TAG_12 = 11;
    private static final int TAG_13 = 12;
    private static final int TAG_14 = 13;
    private static final int TAG_15 = 14;
    private int mintThisFragment = 0;
    private BottomSheet sheet;
    String[] items = new String[]{"From Xml", "Without Icon", "Dark Theme", "Grid", "Style",
            "Style from Theme", "ShareAction", "FullScreen", "Menu Manipulate", "HeaderLayout",
            "From Fragment", "AllCharts"};

    public ResideMenu getmResideMenu() {
        return mResideMenu;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        UltimateBar.newColorBuilder()
                .statusColor(ContextCompat.getColor(this, R.color.color_menu_bg))   // 状态栏颜色
                .applyNav(true)             // 是否应用到导航栏
                .navColor(ContextCompat.getColor(this, R.color.color_menu_bg))         // 导航栏颜色
                .build(this)
                .apply();
        mContext = this;
        initial();
        initData();
        changeFragment(new FloatingActionFragment(), marrList.get(TAG_1));
    }

    @Override
    public void initial() {
        mtvTitleName = (TextView) findViewById(R.id.tv_title_name);
        mbtnBack = (RippleImageView) findViewById(R.id.btn_back);
        mResideMenu = new ResideMenu(mContext);
        mResideMenu.attachToActivity(this);
        mResideMenu.addIgnoredView(findViewById(R.id.fl_fragment));
        mResideMenu.setMenuListener(new OnMenuListener() {
            @Override
            public void openMenu() {
                mbtnBack.setImageResource(R.drawable.ic_navigate_before_white_48dp);
            }

            @Override
            public void closeMenu() {
                mbtnBack.setImageResource(R.drawable.ic_dehaze_white_48dp);
            }
        });

        // valid scale factor is between 0.0f and 1.0f. leftmenu'width is
        // 150dip.
        mResideMenu.setScaleValue(0.5f);
        mMenuAdapter = new MenuAdapter(MainActivity.this);
        mlvListMinu = (ListView) findViewById(R.id.list_minu);
        mlvListMinu.setAdapter(mMenuAdapter);
        mMenuAdapter.setOnClicker(new OnClicker() {

            @Override
            public void onclick(int position) {
                switch (position) {
                    case TAG_1:
                        if (mintThisFragment != TAG_1) {
                            mintThisFragment = TAG_1;
                            changeFragment(new FloatingActionFragment(), marrList.get(TAG_1));
                        }
                        break;
                    case TAG_2:
                        if (mintThisFragment != TAG_2) {
                            mintThisFragment = TAG_2;
                            changeFragment(new FloatLabelFragment(), marrList.get(TAG_2));
                        }
                        break;
                    case TAG_3:
                        if (mintThisFragment != TAG_3) {
                            mintThisFragment = TAG_3;
                            changeFragment(new Lock9Fragment(), marrList.get(TAG_3));
                        }
                        break;
                    case TAG_4:
                        if (mintThisFragment != TAG_4) {
                            mintThisFragment = TAG_4;
                            changeFragment(new CalendarFragment(), marrList.get(TAG_4));
                        }
                        break;
                    case TAG_5:
                        if (mintThisFragment != TAG_5) {
                            mintThisFragment = TAG_5;
                            changeFragment(new BottomSheetFragmentDemo(), marrList.get(TAG_5));
                        }
                        break;
                    case TAG_6:
                        if (mintThisFragment != TAG_6) {
                            mintThisFragment = TAG_6;
                            changeFragment(new BottomBarFragment(), marrList.get(TAG_6));
                        }
                        break;
                    case TAG_7:
                        if (mintThisFragment != TAG_7) {
                            mintThisFragment = TAG_7;
                            changeFragment(new CardsFragment(), marrList.get(TAG_7));
//							Intent intent = new Intent();
//							intent.setClass(mContext, NativeMenuActivity.class);
//							startActivity(intent);
                        }
                        break;
                    case TAG_8:
                        if (mintThisFragment != TAG_8) {
                            mintThisFragment = TAG_8;
                            changeFragment(new DialogsFragment(), marrList.get(TAG_8));
                        }
                        break;

                    case TAG_9:
                        if (mintThisFragment != TAG_9) {
                            mintThisFragment = TAG_9;
                            changeFragment(new ProgressFragment(), marrList.get(TAG_9));
                        }
                        break;
                    case TAG_10:
                        if (mintThisFragment != TAG_10) {
                            mintThisFragment = TAG_10;
                            changeFragment(new TreeFragment(), marrList.get(TAG_10));
                        }
                        break;
                    case TAG_11:
                        if (mintThisFragment != TAG_11) {
                            mintThisFragment = TAG_11;
                            changeFragment(new SnackBarFragment(), marrList.get(TAG_11));
                        }
                        break;
                    case TAG_12:
                        if (mintThisFragment != TAG_12) {
                            mintThisFragment = TAG_12;
                            changeFragment(new ToastFragment(), marrList.get(TAG_12));
                        }
                        break;
                    case TAG_13:
                        Intent intent = new Intent();
                        intent.setClass(mContext, com.smarttablayout.demo.MainActivity.class);
                        startActivity(intent);
                        break;
                    case TAG_14:
                        if (mintThisFragment != TAG_14) {
                            mintThisFragment = TAG_14;
                            changeFragment(new SpinnerFragment(), marrList.get(TAG_14));
                        }
                        break;
                    case TAG_15:
                        if (mintThisFragment != TAG_15) {
                            mintThisFragment = TAG_15;
                            changeFragment(new AllChartFragment(), marrList.get(TAG_15));
                        }
                        break;
                }
                mResideMenu.closeMenu();

            }
        });
        mbtnBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mResideMenu.isOpened()) {
                    mResideMenu.closeMenu();
                } else {
                    mResideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
                }

            }
        });
    }

    @Override
    public void initData() {
        marrList = new ArrayList<String>();
        marrList.add("FloatingActionButton");
        marrList.add("FloatLabel");
        marrList.add("Lock9View");
        marrList.add("Calendar");
        marrList.add("BottomSheet");
        marrList.add("BottomBarNav");
        marrList.add("Cards");
        marrList.add("Dialog");
        marrList.add("Progress");
        marrList.add("Tree");
        marrList.add("SnackBar");
        marrList.add("Toast");
        marrList.add("Multiply Tab");
        marrList.add("Spinner");
        marrList.add("AllCharts");
        mMenuAdapter.setList(marrList);
    }

    private void changeFragment(Fragment targetFragment, String string) {
        mResideMenu.clearIgnoredViewList();
        mtvTitleName.setText(string);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_fragment, targetFragment)
                .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
    }

    @SuppressWarnings("deprecation")
    @Nullable
    @Override
    public Dialog onCreateDialog(final int position, Bundle args) {
        switch (position) {
            case 0:
                sheet = new BottomSheet.Builder(MainActivity.this).icon(getRoundedBitmap(R.drawable.ic_launcher)).title("To " + items[position]).sheet(R.menu.list).listener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.this.onClick(items[position], which);
                    }
                }).build();
                break;
            case 1:
                sheet = new BottomSheet.Builder(MainActivity.this).sheet(R.menu.noicon).listener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.this.onClick(items[position], which);
                    }
                }).build();
                break;

            case 2:
                sheet = new BottomSheet.Builder(MainActivity.this).darkTheme().title("To " + items[position]).sheet(R.menu.list).listener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.this.onClick(items[position], which);
                    }
                }).build();
                break;
            case 3:
                sheet = new BottomSheet.Builder(MainActivity.this).sheet(R.menu.list).listener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.this.onClick(items[position], which);
                    }
                }).grid().build();
                break;
            case 4:
                sheet = new BottomSheet.Builder(MainActivity.this, R.style.BottomSheet_StyleDialog).title("To " + items[position]).sheet(R.menu.list).listener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.this.onClick(items[position], which);
                    }
                }).build();
                break;
            case 5:
                sheet = new BottomSheet.Builder(MainActivity.this).title("To " + items[position]).sheet(R.menu.longlist).listener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.this.onClick(items[position], which);
                    }
                }).limit(R.integer.bs_initial_list_row).build();
                break;
            case 6:
                sheet = getShareActions("Hello " + items[position]).title("Share To " + items[position]).limit(R.integer.no_limit).build();
                break;
            case 7:
                sheet = getShareActions("Hello " + items[position]).title("Share To " + items[position]).build();
                break;
            case 8:
                sheet = new BottomSheet.Builder(MainActivity.this).icon(getRoundedBitmap(R.drawable.ic_launcher)).title("To " + items[position]).sheet(R.menu.list).listener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.this.onClick(items[position], which);
                    }
                }).build();
                final Menu menu = sheet.getMenu();
                menu.getItem(0).setTitle("MenuClickListener");
                menu.getItem(0).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        alert("OnMenuItemClickListener", "You can set OnMenuItemClickListener for each item");
                        return true;
                    }
                });
                menu.getItem(1).setVisible(false);
                menu.getItem(2).setEnabled(false);
                menu.add(Menu.NONE, 23, Menu.NONE, "Fresh meal!");
                menu.findItem(23).setIcon(R.drawable.perm_group_user_dictionary);
                menu.findItem(23).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        toast("Hello");
                        return true;
                    }
                });
                menu.setGroupVisible(android.R.id.empty, false);
                break;
            case 9:
                sheet = new BottomSheet.Builder(MainActivity.this, R.style.BottomSheet_CustomizedDialog).grid().title("To " + items[position]).sheet(R.menu.list).listener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.this.onClick(items[position], which);
                    }
                }).build();
                sheet.setOnShowListener(new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        toast("I'm showing");
                    }
                });
                sheet.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        toast("I'm dismissing");
                    }
                });
                break;
        }

        return sheet;
    }

    private BottomSheet.Builder getShareActions(String text) {
        final Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, text);

        return BottomSheetHelper.shareAction(MainActivity.this, shareIntent);
    }


    void onClick(String name, int which) {
        switch (which) {
            case R.id.share:
                toast("Share to " + name);
                break;
            case R.id.upload:
                toast("Upload for " + name);
                break;
            case R.id.call:
                toast("Call to " + name);
                break;
            case R.id.help:
                toast("Help me!");
                break;
        }
    }

    private Drawable getRoundedBitmap(int imageId) {
        Bitmap src = BitmapFactory.decodeResource(getResources(), imageId);
        Bitmap dst;
        if (src.getWidth() >= src.getHeight()) {
            dst = Bitmap.createBitmap(src, src.getWidth() / 2 - src.getHeight() / 2, 0, src.getHeight(), src.getHeight());
        } else {
            dst = Bitmap.createBitmap(src, 0, src.getHeight() / 2 - src.getWidth() / 2, src.getWidth(), src.getWidth());
        }
        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), dst);
        roundedBitmapDrawable.setCornerRadius(dst.getWidth() / 2);
        roundedBitmapDrawable.setAntiAlias(true);
        return roundedBitmapDrawable;
    }

    private void toast(String str) {
        Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
    }

    private void alert(String str, String str1) {
        Toast.makeText(MainActivity.this, str + str1, Toast.LENGTH_LONG).show();
    }

    public void show(int position) {
        showDialog(position);
    }

}
